<template>
	<a-drawer
		:title="formData.id ? '编辑' : '增加'"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ paddingBottom: '80px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="标题：" name="title">
				<a-input v-model:value="formData.title" placeholder="请输入标题" allow-clear />
			</a-form-item>
			<a-form-item label="积分类型：" name="bonusType">
				<a-select
					disabled
					v-model:value="formData.bonusType"
					:options="bonusTypeOptions"
					placeholder="请选择积分类型"
				/>
			</a-form-item>
			<div>
				<div class="ant-form-item-label block">活动日期：</div>
				<a-space>
					<a-form-item name="startTime">
						<a-date-picker format="YYYY-MM-DD" v-model:value="formData.startTime"></a-date-picker>
					</a-form-item>
					<a-form-item >
						至
					</a-form-item>
					<a-form-item name="endTime">
						<a-date-picker format="YYYY-MM-DD" v-model:value="formData.endTime"></a-date-picker>
					</a-form-item>
				</a-space>
			</div>
			<a-form-item label="活动星期：" name="weeks">
				<a-checkbox-group v-model:value="formData.weeks" :options="weekOptions" />
			</a-form-item>
			<a-form-item label="活动日期：" name="days">
				<a-checkbox-group v-model:value="formData.days" :options="dayOptions" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-drawer>
</template>

<script setup name="crmBonusRuleForm">
	import { message } from 'ant-design-vue'
	import {required} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	import tool from "@/utils/tool";
	import dayjs from "dayjs";
	// 默认是关闭状态
	let visible = $ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		formData.value = {
			cardProp: '1',
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
			if (formData.value.startTime) {
				formData.value.startTime = dayjs(formData.value.startTime)
			}
			if (formData.value.endTime) {
				formData.value.endTime = dayjs(formData.value.endTime)
			}
			formData.value.weeks = formData.value.startWeek.split(',')
			formData.value.days = formData.value.startDay.split(',')
			console.log(formData.value)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible = false
	}

	// 默认要校验的
	const formRules = {
		title: [required('请输入标题')],
		startTime: [required('请选择开始日期')],
		endTime: [required('请选择结束日期')],
	}

	let bonusTypeOptions = tool
		.dictTypeList('BONUS_TYPE')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})

	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}
	const { proxy } = getCurrentInstance()
	let categoryOptions = proxy.$TOOL
		.dictTypeList('CARD_PROP')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})

	let weekOptions = [
		{ label: '周一', value: '1' },
		{ label: '周二', value: '2' },
		{ label: '周三', value: '3' },
		{ label: '周四', value: '4' },
		{ label: '周五', value: '5' },
		{ label: '周六', value: '6' },
		{ label: '周日', value: '7' },
	]

	let dayOptions = []

	for (let i = 1; i <= 31; i++) {
		dayOptions.push({ label: (i+'').padStart(2, '0'), value: i + ''})
	}


	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const param = Object.assign({}, formData.value)
				param.startWeek = param.weeks.join(',')
				param.startDay = param.days.join(',')
				submitLoading.value = true
				crmApi.bonusConfigSubmitForm(param, param.id).then(() => {
					visible = false
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
